rm(list = ls())
library(tidyverse)
library("gridExtra")
# working directory here
setwd("")

# import estimates
df <- read_csv('code/estimates.csv') %>%
  mutate(upper = coef + std * 1.96) %>%
  mutate(lower = coef - std * 1.96)

# data sets
df.main <- df %>%
  filter(group == "All")
df.know <- df %>%
  filter(group == "Knowledge")
df.int <- df %>%
  filter(group == "Interest")


# main estimates
plot.data <- df.main

ggplot(plot.data, aes(x=index, y=coef)) +
  geom_errorbar(
    aes(ymin = lower, ymax = upper, color = "red"),
    width = 0.05) +
  coord_flip() + labs(y='Estimate', x = 'Coefficient') +
  scale_x_reverse(breaks=1:nrow(plot.data), 
                  labels=plot.data$varname) +
  geom_point(aes(color = "red")) +
  ylim(-0.1, 0.3) +
  geom_hline(yintercept=0, color='black', 
             linetype='dashed', alpha=.5) +
  scale_color_manual(values = "red", guide = FALSE) +
  theme_classic() +
  theme(legend.position = "none") 
ggsave("results/est_main.pdf")

# political knowledge
plot.data <- df.know

ggplot(plot.data, aes(x=index, y=coef)) +
  geom_errorbar(
    aes(ymin = lower, ymax = upper, color = sample),
    position = position_dodge(0.2), width = 0.1) +
  coord_flip() + labs(y='Estimate', x = 'Coefficient') +
  scale_x_reverse(breaks=1:nrow(plot.data), 
                  labels=plot.data$varname) +
  geom_point(aes(color = sample, shape = sample), 
             position = position_dodge(0.2)) +
  scale_color_manual(values = c("blue", "red"),
                     name = "Political\n Knowedge") +
  scale_shape_manual(values=c(15, 16),
                     name = "Political\n Knowedge") +
  geom_hline(yintercept=0, color='black', 
             linetype='dashed', alpha=.5) +
  ylim(-0.4,0.9) +
  scale_y_continuous(breaks=seq(-0.4,0.9,0.2)) +
  theme_classic() +
  theme(legend.position = "right", 
        legend.title=element_text(size=8))

ggsave("results/est_know.pdf")

# political interest
plot.data <- df.int

ggplot(plot.data, aes(x=index, y=coef)) +
  geom_errorbar(
    aes(ymin = lower, ymax = upper, color = as.factor(sample_g)),
    position = position_dodge(0.2), width = 0.1) +
  coord_flip() + labs(y='Estimate', x = 'Coefficient') +
  scale_x_reverse(breaks=1:nrow(plot.data), 
                  labels=plot.data$varname) +
  geom_point(aes(color = as.factor(sample_g), shape = as.factor(sample_g)), 
             position = position_dodge(0.2)) +
  scale_color_manual(labels = c("High", "Low"), values = c("blue", "red"), name = "Political\n Interest") +
  scale_shape_manual(labels = c("High", "Low"), values=c(15, 16), name = "Political\n Interest") +
  ylim(-0.3,0.91) +
  geom_hline(yintercept=0, color='black', 
             linetype='dashed', alpha=.5) +
  scale_y_continuous(breaks=c(-0.3,0,0.3,0.6,0.9), limits = c(-0.3,0.9)) +  
  theme_classic() +
  theme(legend.position = "right", 
        legend.title=element_text(size=8))

ggsave("results/est_int.pdf")
